﻿var isUser;


function searchPhone() {
    var phone = $("#txtSearchInput").val();
    $.get("SearchHandler.ashx?toSearch=" + phone, onSuccess);
}
function searchPhoneFromTree(id, attribute) {

    var nodeLink = document.getElementById(id).innerHTML.toString();
         $.get("searchHandler.ashx?toSearch=" + nodeLink, onSuccess);
   
}

function onSuccess(dataAsJson) {
    var dataToDisplay = JSON.parse(dataAsJson);
    $("#ResultsDiv").hide();
    $("#ResultsDiv").empty();
    if (dataToDisplay.length > 0) {
        for (var i = 0; i < dataToDisplay.length; i++) {
            var addToCartUrl;
            if (isUser == true) {
                addToCartUrl = "UserPages/ShopingCart.aspx?ProductID=" + dataToDisplay[i].Id;
            }
            else {
                addToCartUrl = "Account/Login.aspx";
            }
            $("#ResultsDiv").append(
                "<div class=\"col-sm-4 col-md-4\">" +
                          "<div class=\"thumbnail\">" +
                                "<img width=\"250\" height=\"250\" class=\"img-thumbnail\" src=\"" + dataToDisplay[i].Url + "\"/>" +
                                "<div class=\"caption text-center\">" +
                                    "<h3>" + dataToDisplay[i].Name + "</h3>" +
                                    "<p><strong>Price: </strong>" + dataToDisplay[i].Price + " NIS</p>" +
                                    "<p class=\"text-center\">" +
                                        "<a href=\"" + addToCartUrl + "\" class=\"btn btn-danger\" role=\"button\"><span class=\"glyphicon glyphicon-gift\"></span> " +
                                            "Add to Cart</a> <a href=\"Info.aspx?ProductID=" + dataToDisplay[i].Id + "\" class=\"btn btn-primary\" role=\"button\"><span class=\"glyphicon glyphicon-info-sign\"></span>" +
                                            " More Info</a>" +
                                    "</p>" +
                                "</div>" +
                           "</div>" +
                        "</div>");
        }
    }
    else {
        $("#ResultsDiv").append("<h3 class=\"error-message\">No items were found, try to type somthing else.</h3>");
    }
    $("#ResultsDiv").show("slow");
}
function onFailed(res) {
    alert(res.get_message());
}